package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.Node;
import com.heima.pojo.Region;
import com.heima.pojo.VendingMachine;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.VendingMachineDto;
import com.heima.pojo.vo.NodeDetailVo;
import com.heima.pojo.vo.RegionVo;
import com.heima.pojo.vo.VendingMachineVO;
import com.heima.pojo.vo.VmTypeVo;
import com.heima.service.NodeService;
import com.heima.service.RegionService;
import com.heima.service.VendingMachineService;
import com.heima.service.VmTypeService;
import com.heima.utils.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 赵超
 */
@Service
@RequiredArgsConstructor
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements VendingMachineService {
    private final VendingMachineMapper vendingMachineMapper;
    private final RegionService regionService;
    private final VmTypeService vmTypeService;
    private final NodeService nodeService;

    @Override
    public int save(VendingMachineDto dto) {
        VendingMachine vendingMachine = new VendingMachine();
        BeanUtils.copyProperties(dto, vendingMachine);
        return vendingMachineMapper.insert(vendingMachine);
    }

    @Override
    public PageResult<VendingMachineVO> search(Integer pageIndex, Integer pageSize, String innerCode) {
        //pageHelp
        PageHelper.startPage(pageIndex, pageSize);


        //查询处VendingMachine集合并转为VendingMachineVO集
        LambdaQueryWrapper<VendingMachine> lqw = Wrappers.lambdaQuery();
        if (ObjectUtils.isNotEmpty(innerCode)) {
            lqw.eq(VendingMachine::getInnerCode, innerCode);
        }
        List<VendingMachine> vmList = vendingMachineMapper.selectList(lqw);

        List<VendingMachineVO> vmVOList = new ArrayList<>();
        vmList.forEach(vm -> {
            VendingMachineVO vendingMachineVO = new VendingMachineVO();
            BeanUtils.copyProperties(vm, vendingMachineVO);

            //查询VmType并转成VmTypeVO
            VmTypeVo vmTypeVO = getVmTypeVo(vm);

            //查询Region并转成RegionVO
            RegionVo regionVo = getRegionVo(vm);

            //查询Node并转成NodeDetailVO
            NodeDetailVo nodeDetailVO = getNodeDetailVo(vm, regionVo);

            vendingMachineVO.setType(vmTypeVO);
            vendingMachineVO.setRegion(regionVo);
            vendingMachineVO.setNode(nodeDetailVO);
            vmVOList.add(vendingMachineVO);

        });

        // 获取分页结果对象（包含总条数等信息）
        PageInfo<VendingMachine> pageInfo = new PageInfo<>(vmList);


        // 计算总页数
        long totalCount = pageInfo.getTotal();
        // 向上取整
        int totalPage = (int) ((totalCount + pageSize - 1) / pageSize);
        return new PageResult<>(
                pageIndex,
                pageSize,
                totalPage,
                totalCount,
                vmVOList
        );
    }

    private NodeDetailVo getNodeDetailVo(VendingMachine vm, RegionVo regionVo) {
        NodeDetailVo nodeDetailVO = new NodeDetailVo();
        LambdaQueryWrapper<Node> nodeLqw = Wrappers.lambdaQuery();
        Node one = nodeService.getOne(nodeLqw.eq(Node::getId, vm.getNodeId()));
        BeanUtils.copyProperties(one, nodeDetailVO);
        nodeDetailVO.setRegion(regionVo);
        return nodeDetailVO;
    }

    private RegionVo getRegionVo(VendingMachine vm) {
        RegionVo regionVo = new RegionVo();
        LambdaQueryWrapper<Region> regionLqw = Wrappers.lambdaQuery();
        regionService.getOne(regionLqw.eq(Region::getId, vm.getRegionId()));
        return regionVo;
    }

    private VmTypeVo getVmTypeVo(VendingMachine vm) {
        LambdaQueryWrapper<VmType> vmTypeLqw = Wrappers.lambdaQuery();
        VmType vmType = vmTypeService.getOne(vmTypeLqw.eq(VmType::getTypeId, vm.getVmType()));
        VmTypeVo vmTypeVO = new VmTypeVo();
        BeanUtils.copyProperties(vmType, vmTypeVO);
        return vmTypeVO;
    }


}
